package leetcode.editor.cn.q1_300.q100;

import java.util.Arrays;

/**
 * 依旧是抄的答案
 *
 * @author qidi
 * @date 2022-01-12 14:57:21
 */
class Q0132_PalindromePartitioningIi {

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int minCut(String s) {
            int n = s.length();
            boolean[][] flags = new boolean[n][n];
            for (int i = 0; i < n; i++) {
                Arrays.fill(flags[i], true);
            }

            for (int i = n - 1; i >= 0; i--) {
                for (int j = i + 1; j < n; j++) {
                    flags[i][j] = s.charAt(i) == s.charAt(j) && flags[i + 1][j - 1];
                }
            }

            int[] ans = new int[n];
            Arrays.fill(ans, Integer.MAX_VALUE);

            for (int i = 0; i < n; i++) {
                if (flags[0][i]) {
                    ans[i] = 0;
                } else {
                    for (int j = 0; j < i; j++) {
                        if (flags[j + 1][i]) {
                            ans[i] = Math.min(ans[i], ans[j] + 1);
                        }
                    }
                }
            }
            return ans[n - 1];
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)


    public static void main(String[] args) {
        Solution solution = new Q0132_PalindromePartitioningIi().new Solution();
        System.out.println(solution.minCut("aabbcaabb"));
    }
}